package cn.krui.project.service.system;

import cn.krui.helper.db.DBHelper;
import cn.krui.helper.db.SQL;
import cn.krui.project.bean.system.sys_menu.sys_menu;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 

import java.util.List;

/**
 * @program: project-system
 * @description: 菜单模块
 * @author: zc
 * @create: 2019-10-11 10:57
 **/
@Service
public class SysMenuService {

	@Autowired
	private DBHelper dbHelper;

	/**
	 * 系统菜单查询
	 */
	public List<sys_menu> selectSysMenuList(sys_menu sysMenu) {
		SQL.SelectSQL sql = SQL.select("*").from("qr_sys_menu").where("pid=0");
		if (!StringUtils.isEmpty(sysMenu.getPid())) {
			sql.where("pid = " + sysMenu.getPid());
		}
		if (!StringUtils.isEmpty(sysMenu.getLevel())) {
			sql.where("level = " + sysMenu.getLevel());
		}
		if (!StringUtils.isEmpty(sysMenu.getMenu_name())) {
			sql.where("menu_name = " + sysMenu.getMenu_name());
		}
		if (sysMenu.getMenu_type() != null) {
			sql.where("menu_type = " + sysMenu.getMenu_type());
		}
		if (sysMenu.getUser_type() != null) {
			sql.where("user_type = " + sysMenu.getUser_type());
		}
		sql.orderBy("sort desc");
		List<sys_menu> sysMenuList = dbHelper.select(sql, sys_menu.class);
		return sysMenuList;
	}

	/**
	 * 查询所有菜单 zc 2019-12-05 不要改排序
	 * 
	 * @return
	 */
	public List<sys_menu> selectSysMenuListAll() {
		SQL.SelectSQL sql = SQL.select("*").from("qr_sys_menu")
				.orderBy("CASE pid WHEN 0 THEN 1 ELSE 0 END,menu_type,sort");
		List<sys_menu> sysMenuList = dbHelper.select(sql, sys_menu.class);
		return sysMenuList;
	}

	/**
	 * 添加菜单
	 * 
	 * @param sysDept
	 * @return
	 */
	public Integer addSysMenu(sys_menu sysMenu) {
		Object i = dbHelper.insert("qr_sys_menu", sysMenu, sys_menu.class);
		return Integer.valueOf(i.toString());
	}

	/**
	 * 更新菜单
	 * 
	 * @return
	 */
	public Integer updateSysMenu(sys_menu sysMenu) {
		int i = dbHelper.update("qr_sys_menu", sysMenu, sys_menu.class, "id=#{id}");
		return i;
	}

	/**
	 * 删除菜单
	 * 
	 * @return
	 */
	public Integer deleteSysMenu(String id) {
		SQL.DeleteSQL sql = SQL.delete("qr_sys_menu").where("id=#{id}").addParameter("id", id);
		int i = dbHelper.delete(sql);
		return i;
	}

}
